package shayu.cat.controller;



import cn.hutool.crypto.Mode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import shayu.cat.service.CartService;
import shayu.cat.vo.CartItemVo;
import shayu.cat.vo.CartVo;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Component
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    // 获取用户购物车商品项
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItemVo>  getCurrentUserItem(){
         return cartService.getUserCartItems();

    }

    // 添加商品到购物车
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId")Long skuId,
                            @RequestParam("num")Integer num,
                            Model model) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, num);
        model.addAttribute("skuId",skuId);
        return "redirect:localhost:8059/addToCartSuccessPage.html";
    }

    // 跳转到成功页
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId")Long skuId, Model model){
        CartItemVo cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem",cartItem);
        return "success";
    }

    // 改变购物车数量
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId")Long skuId,
                            @RequestParam("num")Integer num){
        cartService.changeItemCount(skuId,num);
        return "redirect:localhost:8059/cart.html";
    }

    // 获取选中的购物车
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId")Long skuId,
                            @RequestParam("checked")Integer checked){
        cartService.checkItem(skuId,checked);
        return "redirect:localhost:8059/cart.html";
    }

    // 删除购物车
    @GetMapping("/deleteItem")
    public String delteItem(@RequestParam("skuId")Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:8059/Cart.html";
    }

    //去购物车页面的请求
    @GetMapping("/cartList.html")
    public String cartList(Model model){
        // 得到用户信息
        CartVo cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }


}
